/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.config.Global;
import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.ExpertsInfo;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.ProExperts;
import com.zx.pm.modules.zx.entity.ProTimeNode;
import com.zx.pm.modules.zx.service.ExpertsInfoService;
import com.zx.pm.modules.zx.service.LzdService;
import com.zx.pm.modules.zx.service.ProExpertsService;
import com.zx.pm.modules.zx.service.ProTimeNodeService;
import com.zx.pm.modules.zx.service.TenderSectionService;

/**
 * 项目时间节点Controller
 * 
 * @author forest
 * @version 2019-01-18
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/proTimeNode")
public class ProTimeNodeController extends BaseController {

	@Autowired
	private ProTimeNodeService proTimeNodeService;

	@Autowired
	private LzdService lzdService;

	@Autowired
	private ProExpertsService proExpertsService;

	@Autowired
	private ExpertsInfoService expertsInfoService;
	
	@Autowired
	private TenderSectionService tenderSectionService;

	
	@ModelAttribute
	public ProTimeNode get(@RequestParam(required = false) String lzdId, @RequestParam(required = false) String sectionId) {
		ProTimeNode entity = new ProTimeNode();
		if (StringUtils.isNotBlank(lzdId)) {
			entity.setLzdId(lzdId);
			entity.setSectionId(sectionId);
			entity = proTimeNodeService.get(entity);
		}
		if (entity == null) {
			entity = new ProTimeNode();
		}
		// 流转单ID
		entity.setLzd(lzdService.get(lzdId));
		return entity;
	}

	@RequiresPermissions("zx:proTimeNode:view")
	@RequestMapping(value = { "list", "" })
	public String list(ProTimeNode proTimeNode, HttpServletRequest request, HttpServletResponse response, Model model) {
		Lzd lzd = proTimeNode.getLzd();
		if (lzd == null) {
			lzd = new Lzd();
		}
		lzd.setRemarks(proTimeNode.getRemarks());
		Page<Lzd> page = lzdService.findSectionPage(new Page<Lzd>(request, response), lzd);
		model.addAttribute("lzd", lzd);
		model.addAttribute("page", page);
		return "modules/zx/proTimeNodeList";
	}

	@RequiresPermissions("zx:proTimeNode:view")
	@RequestMapping(value = "form")
	public String form(ProTimeNode proTimeNode, Model model) {
		ProExperts p = new ProExperts();
		p.setLzdId(proTimeNode.getLzdId());
		/*List<ProExperts> peList = proExpertsService.findListByLzdId(p);

		StringBuilder ids = new StringBuilder();
		if (peList != null && peList.size() > 0) {
			for (ProExperts e : peList) {
				if (ids.length() > 0) {
					ids.append(",");
				}
				ids.append(e.getExpertsInfo().getId());
			}
		}*/
		proTimeNode.setSection(tenderSectionService.get(proTimeNode.getSectionId()));
		//proTimeNode.setDepositMoney(lzdService.getDepositMoney(proTimeNode.getLzdId(), proTimeNode.getSectionId()));
		//model.addAttribute("peList", peList);
		//model.addAttribute("ids", ids.toString());
		model.addAttribute("proTimeNode", proTimeNode);
		return "modules/zx/proTimeNodeForm";
	}

	@RequiresPermissions("zx:proTimeNode:view")
	@RequestMapping(value = "show")
	public String show(ProTimeNode proTimeNode, Model model) {
		ProExperts p = new ProExperts();
		p.setLzdId(proTimeNode.getLzdId());
		List<ProExperts> peList = proExpertsService.findListByLzdId(p);

		StringBuilder ids = new StringBuilder();
		if (peList != null && peList.size() > 0) {
			for (ProExperts e : peList) {
				if (ids.length() > 0) {
					ids.append(",");
				}
				ids.append(e.getExpertsInfo().getId());
			}
		}
		proTimeNode.setSection(tenderSectionService.get(proTimeNode.getSectionId()));
		//proTimeNode.setDepositMoney(lzdService.getDepositMoney(proTimeNode.getLzdId(), proTimeNode.getSectionId()));
		model.addAttribute("peList", peList);
		model.addAttribute("ids", ids.toString());
		model.addAttribute("pt", proTimeNode);
		return "modules/zx/proTimeNodeShow";
	}
	
	@RequiresPermissions("zx:proTimeNode:edit")
	@RequestMapping(value = "save")
	public String save(ProTimeNode proTimeNode, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proTimeNode)) {
			return form(proTimeNode, model);
		}
		proTimeNodeService.save(proTimeNode);
		// 保存评审专家信息
		//proExpertsService.deleteByLzdId(proTimeNode.getLzdId());
		/*if (StringUtils.isNotBlank(proTimeNode.getExpertsIds())) {
			String[] expertsIds = proTimeNode.getExpertsIds().split(",");
			ProExperts pe = null;
			for (String str : expertsIds) {
				pe = new ProExperts();
				pe.setExpertsId(str);
				pe.setLzdId(proTimeNode.getLzdId());
				proExpertsService.save(pe);
			}
		}*/
		addMessage(redirectAttributes, "保存项目时间节点成功");
		return "redirect:" + Global.getAdminPath() + "/zx/proTimeNode/?repage";
	}

	@RequiresPermissions("zx:proTimeNode:edit")
	@RequestMapping(value = "delete")
	public String delete(ProTimeNode proTimeNode, RedirectAttributes redirectAttributes) {
		proTimeNodeService.delete(proTimeNode);
		addMessage(redirectAttributes, "删除项目时间节点成功");
		return "redirect:" + Global.getAdminPath() + "/zx/proTimeNode/?repage";
	}

	@RequiresPermissions("zx:proTimeNode:edit")
	@RequestMapping(value = "myExertsList")
	public String myExertsList(HttpServletRequest request, Model model, ExpertsInfo expertsInfo) {
		// 获取专家信息列表
		List<ExpertsInfo> list = expertsInfoService.findList(expertsInfo);
		// 获取项目供应商列表
		ProExperts pe = new ProExperts();
		pe.setLzdId(request.getParameter("lzdId"));
		List<ProExperts> peList = proExpertsService.findListByLzdId(pe);
		// 判断供应商是否已经分配给该项目了
		for (ExpertsInfo e : list) {
			for (ProExperts p : peList) {
				if (p.getExpertsId().equals(e.getId())) {
					e.setFenPeiFlag("1");
					break;
				}
			}
		}
		model.addAttribute("lzdId", request.getParameter("lzdId"));
		model.addAttribute("peList", list);
		model.addAttribute("expertsInfo", expertsInfo);
		return "modules/zx/myExpertsList";
	}

	@RequestMapping(value = "expertsInfo")
	public String expertsInfo(HttpServletRequest request, Model model) {
		String expertsId = request.getParameter("exertsId");
		ExpertsInfo e = expertsInfoService.get(expertsId);
		model.addAttribute("experts", e);
		return "modules/zx/expertsInfo";
	}
}